package com.dalianpai.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.dalianpai.gulimall.order.vo.PayVo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public static String app_id = "2021000116667682";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQClo7YKk8lnuHsKe4aOLmNd/vCRZ5ia8kpxvSR9iy1REkpfVV1cV1TqQlSj3OZ62PtoHDNbCbgp01gUxe/96PwObjpWudxtVklf8YifTFWPafed0q0blEBayzXt26tX8H/Uxd0oKFmdh+VYLDJ6or3qPfrhd8f298v1Ijrc1ituAgTsHybQ946WpnE+JhTAmCinzxeDrXAOZ96WDyaZB7hxPKu+AvLEU/RJIxKLXyVN51rEeI+kYDNQvscs/8WVXN0Qldkd1bXnB5RpOeEl7D7Q7ZasK4Q9vRnSw1Yu1vtWv5HlUgopfCweix+wkIWK/dFTfF5i5KW0etXJc38vCLw9AgMBAAECggEAOCtcABo9oj6c+uEIsA5rA+UAD07iISkotYk+ZpRbubJKJin8juPOl+IGccrnHaGXH6bwg164ZPaWV/UHfUMUtpAKmH5OkYVBPJfbjKmGGiwFHC/lJAUSDbf7gb2MTTXxI2Y1qPiF6zW6QDw8ofmuDVEXUn4cAVPmhiola930vEjGdCFvY1vTOFw64fECAnE8/zkEF98byluE3DqSFZmUQfMeuu0LYwMcVCiv+qXmBH/I5ojOfXCHkbzUPYCE0OIg4Agu1QMlkA6YXn0d4KW7nrME1BUC2UJTNFsJwP5wg5hWiNOEab8vlsYKIIiFPlO0+VFUWAXPz6IaEFjSMaFewQKBgQDmmZvrLP3QFF3HeE74yKymvM5OOjP1ovKde7jko2fZPUmJst6ef4/VI39BHLTWS/IHc9OpufPfFrObgf2vbaLvc+xzQcz5EeuH+haYYIF6PFLAvhpl6+nv8BNh8nlZjr7GCqOqKR+gfPfJHakA2q1H2if1c4HLsyTgUc2VurxcOwKBgQC34l05vgSK2vDlgj9haWohtJBSZl+iHuMb9+Df6Dhc81ugUKx1QjQApyYTAaxUDGWY6NxQ7NSWjO4qcXM5KDL/GJhFRRC5qZDNERQ2jPpieqIq0BiYWYGOn57V3wNOFU8xk8rKMVhiGATcHpPG9bHP6a7uQ65TlX7DTuImhfNZ5wKBgQCPtEBXQXV2m4qJiO7l2ODHE7gZReoldINvIq/pQ++ukKPoPODAs0vmDjX6VLJZLYDJpiPMjIZuiw4opcx6637zbDF47hOXT9SQq4gzM0jc7QTA+ncm9uqVTO43Rszaw7sdXXn6y5/a+qXL3kD4uE3uDgSC0JWdZKlhZXkRiwphCwKBgQC2GYii9yc+b0Vis5KaoC8HQ+4se883GIlmGBrtAeZXPnS9lUbENJSVFPJiniSPc1tliy0rQu3gTZ5KD+HAd6vQ7gVVi+jdRCvuuHwg7YnD3pgb98pzjSf9GtvMBrcaK4YqnaaUhzt0oT6m0e0cehHlyRCASiwoAALx3XueFMcCfwKBgQCrFBEOO04bWDX0pTrA2OU8FtvoXAUl2ji3FOXC4sUxgFnT9dFw/PlpGtx9DjYcspvHC+cNAYkxAUepePXkZbj8SSOgHjsdKouSM+Nwo9+93DGN29YUO6l6AIt6IPxXvSGUTQUCPlBQ2A1y4YFEjg5uxu85u3AYqa6a4qYTVMFKeQ==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkTof8EkrceHNaJcuqPx7aAUFPJ1X3WBwf9wDgPlQ2U/lnNCd6z196azAYpR9WJSqZv9iJY9TFVBIr+XHr4hFsdIgyb3ZkH6MIs3x2yahMij+3msUY34NQkrz3S90Fb1FfTFYoANOxwhP2Q7QyfNyJkZgcVvCnf66lsGt/E1eXxUP8xpVcHUgWVHp+orGlMXkGsC8XuHcTTFCAdhA+NHHjDCPLOJ1cWcqDjmSk8WqH+FGEvGt+HwiNxZvImoa4+4CIwlwAv+CayRgjvpqk6H50KNkTP54AVgR1DYCs6nqvoGuj6V+mlEisT9U1Rl5XJxX6JPg0ddGq1rDdFh+wGRM/wIDAQAB";
    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://47.110.91.150/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    public static String sign_type = "RSA2";

    // 字符编码格式
    public  static String charset = "utf-8";

    // 支付宝网关
    public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
